#ifndef DETECTSERVER_HPP
#define DETECTSERVER_HPP

#include "detectresult.hpp"
#include "doublebf2.hpp"
#include "server.hpp"

//#define OUTHLOG

class DetectServer : public ServerBase
{
public:
    DoubleBf2<DetectResultS> humanBuffer;
    std::thread listenThread;
#ifdef OUTHLOG
    std::ofstream ofs;
#endif
public:
    DetectServer()
        : ServerBase()
    #ifdef OUTHLOG
        , ofs("hlog.txt")
    #endif
    {

    }

    virtual void task(char buffer[], std::string &response)
    {
        DetectResultS *pd=new DetectResultS();
        pd->Decode1(std::string(buffer));
        humanBuffer.Write(pd);
        response="show qr code\n";
#ifdef OUTHLOG
        std::cout<<"end detect task\n"<<std::flush;

        ofs<<buffer<<"\n";
#endif
    }

    static void startThread(DetectServer *pData, uint16_t portno=4567)
    {
        pData->start2(portno, 2048);
    }

    void Start(uint16_t portno=7654)
    {
        listenThread=std::thread(startThread, this, portno);
    }

    void End()
    {
        go=false;
        if(listenThread.joinable())
            listenThread.join();
#ifdef OUTHLOG
        ofs.close();
#endif
    }


    void test(uint16_t portno=4567)
    {
        cv::namedWindow("angle",cv::WINDOW_NORMAL);
        cv::moveWindow("angle",32,32);
        cv::resizeWindow("angle", 1280, 720);

        Start(portno);
        int k=0;
        while(k!=27)
        {
            DetectResultS *pResult1=NULL;
            humanBuffer.Read(pResult1);
            cv::Mat canvas=pResult1->DPMat();
            cv::imshow("angle", canvas);
            k=cv::waitKey(1);
            delete pResult1;
        }
        End();
    }

};


#endif // DETECTSERVER_HPP

